// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <gtest/gtest.h>
#include <pollux/core/core_type_system.h>

using namespace kumo::pollux::core;
using namespace kumo::pollux;

namespace {
StringWriter createStringWriter(const std::string& str) {
  StringWriter writer;
  UDFOutputString::assign(writer, str);
  return writer;
}
} // namespace

TEST(String, StringWriter) {
  // Default constructor & empty string copy.
  {
    const std::string emptyString;
    StringWriter writer;
    UDFOutputString::assign(writer, emptyString);
    EXPECT_EQ(StringView(writer), StringView(emptyString));
  }

  const std::string sampleTest{"short string"};
  // Copy constructor
  {
    const auto src = createStringWriter(sampleTest);
    StringWriter dst(src);
    EXPECT_NE(dst.data(), src.data());
    EXPECT_EQ(StringView(dst), StringView(sampleTest));
  }

  // Move constructor
  {
    StringWriter dst(createStringWriter(sampleTest));
    EXPECT_EQ(StringView(dst), StringView(sampleTest));
  }

  // Copy assignment
  {
    const auto src = createStringWriter(sampleTest);
    StringWriter dst;
    dst = src;
    EXPECT_NE(dst.data(), src.data());
    EXPECT_EQ(StringView(dst), StringView(sampleTest));
  }

  // Move assignment
  {
    StringWriter dst;
    dst = createStringWriter(sampleTest);
    EXPECT_EQ(StringView(dst), StringView(sampleTest));
  }
}
